package thread_pool;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/23
 * Time:15:16
 * withInitial初始化方法
 **/
public class PoolDemo26 {
    /*static ThreadLocal<String> threadLocal = ThreadLocal.withInitial(new Supplier<String>() {
        @Override
        public String get() {
            System.out.println("执行了初始化方法");
            return "java";
        }
    });*/

    //
    static ThreadLocal<String> threadLocal = ThreadLocal.withInitial(()->"java");

    public static void main(String[] args) {
        try {
            String result = threadLocal.get();
            System.out.println("结果:" + result);
        }finally {
            threadLocal.remove();//不可省略
        }

        String result2 = threadLocal.get();
        System.out.println("结果2："+result2);//java
        //有值，所以执行初始化代码
    }
}
